package com.rome.hotel.data.controller;


import com.rome.hotel.common.annotation.Anonymous;
import com.rome.hotel.common.core.domain.AjaxResult;
import com.rome.hotel.common.utils.file.minio.MinIOUtils;

import io.jsonwebtoken.lang.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


@RestController
@Anonymous
@RequestMapping("/file")
public class CommonFileUpOrDown {

    public static String imgNmae = null;
    /**
     * 文件上传手动
     * @param file
     * @return
     */

    @PostMapping("/upload/noAuto")
    public AjaxResult uploadNoAuto(MultipartFile file){
        Assert.state(file.getSize() <= 104857600,"文件上传过大");
        try {
            String fileUrl = MinIOUtils.uploadFile(file);
            CommonFileUpOrDown.imgNmae = fileUrl;
            return AjaxResult.success(fileUrl,"文件上传成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.success("文件上传异常，请联系管理员");
        }
    }

    /**
     * 文件上传
     * @param file
     * @return
     */

    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file){
        Assert.state(file.getSize() <= 104857600,"文件上传过大");
        try {
            String fileUrl = MinIOUtils.uploadFile(file);
            return AjaxResult.success(fileUrl,"文件上传成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.success("文件上传异常，请联系管理员");
        }
    }
    /**
     * 文件删除
     * 删除true@param fileName
     * @return
     * @throws IOException
     */
    @DeleteMapping("/delete")
    public AjaxResult fileDelete(String filePath) throws InterruptedException {
        try {
            MinIOUtils.removeFile(filePath);
            return AjaxResult.success(filePath, "删除成功");
        } catch (Exception e) {
            return AjaxResult.error("删除异常，请联系管理员");
        }
    }


    /**
     * 房间描述上传
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/roomDescription/upload")
    public AjaxResult uploadFileRoomDescription(MultipartFile file) throws IOException
    {
        try {
            String uploadFile = MinIOUtils.uploadFile(file);
            return AjaxResult.success(uploadFile);
        } catch (Exception e) {
            return AjaxResult.error("上传图片异常，请联系管理员");
//            throw new RuntimeException(e);
        }
/*        String dir = "roomDescription";//文件的目录
        Assert.state(file.getSize() <= 104857600,"文件上传过大");
        if (!file.isEmpty())
        {
//            图片名
            String imgName = FileLoad.fileUp(file,dir);
            if (!StringUtils.isEmpty(imgName))
            {
                AjaxResult ajax = AjaxResult.success();
                ajax.put("imageUrl", imgName);
                return ajax;
            }
        }
        return AjaxResult.error("上传图片异常，请联系管理员");*/
    }
    /**
     * 房间描述删除
     * 删除true@param fileName
     * @return
     * @throws IOException
     */
    @DeleteMapping("/roomDescription/delete")
    public AjaxResult roomDescriptionDeleImage(String filePath) throws InterruptedException {
        try {
            MinIOUtils.removeFile(filePath);
            return AjaxResult.success(filePath);
        } catch (Exception e) {
            return AjaxResult.error("删除异常，请联系管理员");
        }

    }



}
